﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace AmazonGlacierGUI.UI.Infrastructure.MVVMFramework.Converters
{
    public class BytesToTextConverter : IValueConverter
    {
        public BytesToTextConverter()
        {

        }
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return GetSizeText(long.Parse(value.ToString()));
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
        string GetSizeText(long bytes)
        {            
            //http://stackoverflow.com/questions/281640/how-do-i-get-a-human-readable-file-size-abbreviation-using-net
            string[] sizes = { "B", "KB", "MB", "GB" };
            int order = 0;
            while (bytes >= 1024 && order + 1 < sizes.Length)
            {
                order++;
                bytes = bytes / 1024;
            }

            // Adjust the format string to your preferences. For example "{0:0.#}{1}" would
            // show a single decimal place, and no space.
            return String.Format("{0:0.##} {1}", bytes, sizes[order]);
        }
    }
}
